﻿using DxfMonitor.ViewModel;
using MahApps.Metro.Controls;
using NaturalSort.Extension;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DxfMonitor.View
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : MetroWindow
    {
        public static MainWindow MainWin;
        public MainWindow()
        {
            InitializeComponent();
            MainWin = this;
        }

        private void Grid_MouseMove(object sender, MouseEventArgs e)
        {

        }

        private void Grid_Drop(object sender, DragEventArgs e)
        {

        }


        private static T FindParent<T>(DependencyObject child, string parentName)
where T : DependencyObject
        {
            if (child == null) return null;

            T foundParent = null;
            var currentParent = VisualTreeHelper.GetParent(child);

            do
            {
                var frameworkElement = currentParent as FrameworkElement;
                if (frameworkElement.Name == parentName && frameworkElement is T)
                {
                    foundParent = (T)currentParent;
                    break;
                }

                currentParent = VisualTreeHelper.GetParent(currentParent);

            } while (currentParent != null);

            return foundParent;
        }

        private FolderPairViewModel FindPairByGrid(Grid grid)
        {
            Border lbi = FindParent<Border>(grid, "itemBorder");
            //Console.WriteLine(lbi.DataContext);
            FolderPairViewModel folderPairViewModel = lbi.DataContext as FolderPairViewModel;
            return folderPairViewModel;
        }

        private void Grid_Drop_Last(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
                string dir = files[0];
                if (Directory.Exists(dir))
                {
                    FolderPairViewModel folderPairViewModel = FindPairByGrid(sender as Grid);
                    folderPairViewModel.LastFolderFull = dir;
                }
            }
        }
        private void Grid_Drop_Current(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
                string dir = files[0];
                if (Directory.Exists(dir))
                {
                    FolderPairViewModel folderPairViewModel = FindPairByGrid(sender as Grid);
                    folderPairViewModel.CurrentFolderFull = dir;
                }
            }
        }

        private void Grid_MouseMove_Last(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                DataObject data = new DataObject();
                FolderPairViewModel folderPairViewModel = FindPairByGrid(sender as Grid);
                data.SetData(DataFormats.FileDrop, new string[] { folderPairViewModel.LastFolderFull });
                DragDrop.DoDragDrop(this, data, DragDropEffects.Copy | DragDropEffects.Move);
            }
        }

        private void Grid_MouseMove_Current(object sender, MouseEventArgs e)
        {
            {
                if (e.LeftButton == MouseButtonState.Pressed)
                {
                    DataObject data = new DataObject();
                    FolderPairViewModel folderPairViewModel = FindPairByGrid(sender as Grid);
                    data.SetData(DataFormats.FileDrop, new string[] { folderPairViewModel.CurrentFolderFull });
                    DragDrop.DoDragDrop(this, data, DragDropEffects.Copy | DragDropEffects.Move);
                }
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            ListCollectionView collectionView = CollectionViewSource.GetDefaultView(lvw.ItemsSource) as ListCollectionView;
            collectionView.Filter = UserFilter;
#if DEBUG
            SetCurrentValue(TopmostProperty, false);
#endif


            //collectionView.CustomSort = new NaturalSortComparer(StringComparison.OrdinalIgnoreCase) as IComparer;
            collectionView.CustomSort = new NaturalFolderPairViewModelSorter();



        }
        private bool UserFilter(object item)
        {
            if (chkFilterNull.IsChecked.HasValue && chkFilterNull.IsChecked.Value)
                return true;
            else
                return !string.IsNullOrWhiteSpace((item as FolderPairViewModel).CurrentFolderFull);

        }

        private void chkFilterNull_Checked(object sender, RoutedEventArgs e)
        {
            CollectionViewSource.GetDefaultView(lvw.ItemsSource).Refresh();
        }

        public void BringFolderIntoView()
        {
            MainViewModel mainViewModel = DataContext as MainViewModel;
            FolderPairViewModel selectedVm = mainViewModel.SelectedPairViewModel;
            //lvw.UpdateLayout();
            ListBoxItem selectedItem = lvw.ItemContainerGenerator.ContainerFromItem(selectedVm) as ListBoxItem;
            if (selectedItem != null)
                selectedItem.BringIntoView();
        }

        private void MetroWindow_ContentRendered(object sender, EventArgs e)
        {
            BringFolderIntoView();
        }
    }

    public class NaturalFolderPairViewModelSorter : IComparer
    {
        public static NaturalSortComparer cmp = new NaturalSortComparer(StringComparison.OrdinalIgnoreCase);
        public int Compare(object x, object y)
        {
            FolderPairViewModel xv = x as FolderPairViewModel;
            FolderPairViewModel yv = y as FolderPairViewModel;
            return cmp.Compare(xv.CurrentFolder, yv.CurrentFolder);
        }
    }

    public class NaturalFolderPairViewModelGenericSorter : IComparer<FolderPairViewModel>
    {
        public static NaturalSortComparer cmp = new NaturalSortComparer(StringComparison.OrdinalIgnoreCase);

        public int Compare(FolderPairViewModel x, FolderPairViewModel y)
        {
            return cmp.Compare(x.CurrentFolder, y.CurrentFolder);
        }
    }
}
